﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Collections;
using MySql.Data.MySqlClient;

namespace Apq.DB.MySql
{
	/// <summary>
	/// 将内存中的数据导出为Mysql语句
	/// </summary>
	public class ExportSql
	{
		/// <summary>
		/// 导出行语句(不含括号)
		/// </summary>
		public static string FromArray(object[] ary)
		{
			List<string> lst = new List<string>();
			foreach (object obj in ary)
			{
				lst.Add(Common.ConvertToSqlON(obj));
			}
			string rtn = string.Join(",", lst);
			return rtn;
		}
		/// <summary>
		/// 导出行语句(不含括号)
		/// </summary>
		public static string FromDataRow(System.Data.DataRow dr)
		{
			string rtn = FromArray(dr.ItemArray);
			return rtn;
		}
		/// <summary>
		/// 导出行语句(含括号)
		/// </summary>
		/// <param name="dt"></param>
		/// <param name="Insert">给定“INSERT INTO table_name(colList) VALUES”</param>
		/// <param name="IsBatch">输出语句是否为批量添加(速度快)</param>
		public static string FromDataTable(System.Data.DataTable dt, string Insert = "$请指定“INSERT...VALUES”串$", bool IsBatch = true)
		{
			List<string> lst = new List<string>();
			foreach (System.Data.DataRow dr in dt.Rows)
			{
				string strRow = FromDataRow(dr);
				if (IsBatch)
				{
					lst.Add("(" + strRow + ")");
				}
				else
				{
					lst.Add(Insert + "(" + strRow + ")");
				}
			}
			string rtn = string.Empty;
			if (IsBatch)
			{
				rtn = Insert + string.Join(",", lst) + ";";
			}
			else
			{
				rtn = string.Join(";", lst) + ";";
			}
			return rtn;
		}
	}
}
